package com.ruoyi.web.controller.plat;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PlatTableDataInfo;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.domain.vo.answer.ReqCommonVO;
import com.ruoyi.system.service.IPlatLessonService;
import com.ruoyi.system.service.IPlatUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 课程管理
 */
@RestController
@RequestMapping("/lesson")
public class LessonController extends BaseController {

    @Autowired
    private IPlatLessonService platLessonService;
    @Autowired
    private IPlatUserService platUserService;

    /**
     * 添加课程
     */
    @PostMapping("/addLesson")
    public AjaxResult addLesson(PlatLesson platLesson) {
        platLessonService.addLesson(platLesson);
        AjaxResult ajax = AjaxResult.success();
        return ajax;
    }

    /**
     * 课程列表
     */
    @GetMapping("/lessonList")
    public PlatTableDataInfo lessonList(PlatLesson platLesson,HttpServletRequest request) {
        List<PlatLesson> list = platLessonService.lessonList(platLesson,request);
        return getPlatDataTable(list);
    }

    /**
     * 课程分页列表
     */
    @GetMapping("/lessonPageList")
    public PlatTableDataInfo lessonPageList(PlatLesson platLesson,HttpServletRequest request) {
        startPage();
        List<PlatLesson> list = platLessonService.lessonList(platLesson,request);
        return getPlatDataTable(list);
    }

    /**
     * 删除课程
     */
    @GetMapping("/delLesson/{id}")
    public AjaxResult delLesson(@PathVariable Long id) {
        platLessonService.delLesson(id);
        AjaxResult ajax = AjaxResult.success();
        return ajax;
    }

    /**
     * 课程数量（首页）
     * 已安排课程数量（首页）
     */
    @GetMapping("/lessonHome")
    public AjaxResult lessonHome(HttpServletRequest request) {
        LessonHomeVO lessonHomeVO = platLessonService.lessonHome(request);
        AjaxResult ajax = AjaxResult.success(lessonHomeVO);
        return ajax;
    }

    /**
     * 我的课程列表(教师)
     */
    @GetMapping("/myLessonList")
    public PlatTableDataInfo myLessonList(PlatLesson platLesson, HttpServletRequest request) {
        PlatUser platUser = platUserService.getLoginUser(request);
        startPage();
        List<PlatLesson> list = platLessonService.myLessonList(platLesson, request,platUser);
        return getPlatDataTable(list);
    }

    /**
     * 我的课程列表(学生)
     */
    @GetMapping("/myStudentLessonList")
    public PlatTableDataInfo myStudentLessonList(PlatLesson platLesson, HttpServletRequest request) {
        platLesson.setStudentStateCountans(0);
        //当前用户
        PlatUser loginUser = platUserService.getLoginUser(request);
        startPage();
        List<PlatLessonStudent> list = platLessonService.myStudentLessonList(platLesson, loginUser);
        PlatTableDataInfo platTableDataInfo = getPlatDataTable(list);
        platTableDataInfo.setData(platLessonService.myStudentLessonListData((List<PlatLessonStudent>) platTableDataInfo.getData()));
        return platTableDataInfo;
    }

    /**
     * 我的成绩(学生)
     */
    @GetMapping("/achievementList")
    public PlatTableDataInfo achievementList(PlatLesson platLesson, HttpServletRequest request) {
        platLesson.setStudentStateCountans(1);
        //当前用户
        PlatUser loginUser = platUserService.getLoginUser(request);
        startPage();
        List<PlatLessonStudent> list = platLessonService.myStudentLessonList(platLesson, loginUser);
        return getPlatDataTable(list);
    }


    /**
     * 我的排课列表
     */
    @GetMapping("/myLessonScheduleList")
    public PlatTableDataInfo myLessonScheduleList(PlatUser platUser, HttpServletRequest request) {
        //当前用户
        PlatUser loginUser = platUserService.getLoginUser(request);

        String lessonTypeId = request.getParameter("typeId");
        String name = request.getParameter("name");

        startPage();
        List<PlatLessonSchedule> list = platLessonService.myLessonScheduleList(platUser, loginUser,lessonTypeId,name);

        PlatTableDataInfo platTableDataInfo = getPlatDataTable(list);
        platTableDataInfo = platLessonService.myLessonScheduleListData(platTableDataInfo);
        return platTableDataInfo;
    }

    /**
     * 当前课程中的学生
     */
    @PostMapping("/myLessonStudentList/{scheduleId}")
    public PlatTableDataInfo myLessonStudentList(@PathVariable Long scheduleId, HttpServletRequest request) {
        startPage();
        List<PlatUser> list = platLessonService.myLessonStudentList(scheduleId,request.getParameter("name"));
        PlatTableDataInfo platTableDataInfo = getPlatDataTable(list);
        return platTableDataInfo;
    }

    /**
     * 移除当前课程中的学生
     */
    @GetMapping("/removeLessonStudent/{id}")
    public AjaxResult removeLessonStudent(@PathVariable Long id) {
        platLessonService.removeLessonStudent(id);
        AjaxResult ajax = AjaxResult.success();
        return ajax;
    }

    /**
     * 添加当前课程中的学生
     */
    @GetMapping("/addLessonStudent/{userId}/{scheduleId}/{lessonId}")
    public AjaxResult addLessonStudent(@PathVariable Long userId,@PathVariable Long scheduleId,@PathVariable Long lessonId) {
        platLessonService.addLessonStudent(userId,scheduleId,lessonId);
        AjaxResult ajax = AjaxResult.success();
        return ajax;
    }

    /**
     * 我的排课列表(首页日程)
     */
    @PostMapping("/myLessonFullCalendarList")
    public AjaxResult addLessonSchedule(HttpServletRequest request) {
        List<LessonFullCalendarVO> list = platLessonService.myLessonFullCalendarList(request);
        AjaxResult ajax = AjaxResult.success(list);
        return ajax;
    }

    /**
     * 排课
     */
    @PostMapping("/addLessonSchedule")
    public AjaxResult addLessonSchedule(PlatLessonSchedule platLessonSchedule, HttpServletRequest request) {
        platLessonService.addLessonSchedule(platLessonSchedule, request);
        AjaxResult ajax = AjaxResult.success();
        return ajax;
    }

    /**
     * 删除排课
     */
    @GetMapping("/delLessonSchedule/{id}")
    public AjaxResult delClass(@PathVariable Long id) {
        platLessonService.delLessonSchedule(id);
        AjaxResult ajax = AjaxResult.success();
        return ajax;
    }

    /**
     * 结束排课（学生）
     */
    @GetMapping("/endLesson/{id}")
    public AjaxResult endLesson(@PathVariable Long id) {
        platLessonService.endLesson(id);
        AjaxResult ajax = AjaxResult.success();
        return ajax;
    }

    /**
     * 课程类别
     */
    @GetMapping("/lessonTypeList")
    public PlatTableDataInfo lessonTypeList(PlatLessonType platLessonType,HttpServletRequest request) {
        List<PlatLessonType> list = platLessonService.lessonTypeList(platLessonType,request);
        return getPlatDataTable(list);
    }

    /**
     * 课程类别
     */
    @GetMapping("/lessonTypePageList")
    public PlatTableDataInfo lessonTypePageList(PlatLessonType platLessonType,HttpServletRequest request) {
        startPage();
        List<PlatLessonType> list = platLessonService.lessonTypeList(platLessonType,request);
        return getPlatDataTable(list);
    }

    /**
     * 获取课程类别
     */
    @GetMapping("/lessonType/{id}")
    public AjaxResult lessonTypePageList(@PathVariable Long id) {
        AjaxResult ajax = AjaxResult.success(platLessonService.lessonType(id));
        return ajax;
    }

    /**
     * 存储答题详情
     */
    @PostMapping("/lessonInAnswer")
    public AjaxResult lessonInScore(@RequestBody ReqCommonVO req, HttpServletRequest request) {
        platLessonService.lessonInAnswer(req,request);
        AjaxResult ajax = AjaxResult.success();
        return ajax;
    }

    /**
     * 获取答题详情
     */
    @GetMapping("/getLessonInAnswer/{id}")
    public AjaxResult getLessonInAnswer(@PathVariable Long id,HttpServletRequest request) {
        List<PlatLessonStudentAnswer> answer = platLessonService.getLessonInAnswer(id,request);
        AjaxResult ajax = AjaxResult.success(answer);
        return ajax;
    }

    /**
     * 记录第一次答题时间
     */
    @GetMapping("/savrInitAnsTime/{lessonId}/{platLessonStudentId}")
    public AjaxResult savrInitAnsTime(@PathVariable Long lessonId,@PathVariable Long platLessonStudentId, HttpServletRequest request) {
        platLessonService.savrInitAnsTime(lessonId,platLessonStudentId,request);
        AjaxResult ajax = AjaxResult.success();
        return ajax;
    }

    /**
     * 获取子课程开始状态
     */
    @ApiOperation("获取子课程开始状态")
    @GetMapping("/lessonSonState/{platLessonStudentId}")
    public AjaxResult lessonSonState(@PathVariable Long platLessonStudentId,HttpServletRequest request) {
        Map<String,String> map = platLessonService.lessonSonState(platLessonStudentId,request);
        AjaxResult ajax = AjaxResult.success(map);
        return ajax;
    }

    /**
     * 查询当前类别的标签列表
     */
    @ApiOperation("查询当前类别的标签列表")
    @GetMapping("/getLabelByType/{typeId}")
    public AjaxResult getLabelByType(@PathVariable Long typeId,HttpServletRequest request) {
        List<PlatLessonLabel> list = platLessonService.getLabelByType(typeId,request);
        AjaxResult ajax = AjaxResult.success(list);
        return ajax;
    }

    /**
     * 统计数据
     */
    @PostMapping("/dataStatistics/{typeId}")
    public AjaxResult dataStatistics(@PathVariable Long typeId,HttpServletRequest request) {
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        StatisticalAnalysisVO statisticalAnalysisVO = platLessonService.dataStatistics(typeId,startDate,endDate,request);
        AjaxResult ajax = AjaxResult.success(statisticalAnalysisVO);
        return ajax;
    }

    /**
     * 添加课程
     */
    @PostMapping("/copy/{lessonId}")
    public AjaxResult copy(@PathVariable Long lessonId) {
        platLessonService.copy(lessonId);
        AjaxResult ajax = AjaxResult.success();
        return ajax;
    }
}
